//自动渲染前段组件
var AutoRenderWebCompent={}
/*
<!--单图上传-->
<div data-arwc="upOneImg" class="form-group no-margin-left no-margin-right mt4 short">
	<div class="input-group">
		<input data-arwcId='textImgUrl'  type="text" value="" name="url" class="form-control" placeholder="请直接输入图片地址或使用上传图片按钮上传图片" />
		<span class="input-group-btn">
			<button data-arwcId="btnUpload" type="button" class="btn btn-info btn-sm">上传图片</button>
		</span>
	</div>
	<img data-arwcId="previewImage" style="height: 80px;" class="preview-image" src="http://static.yedadou.com/public/admin/images/noupload.png" onerror="this.src='http://static.yedadou.com/public/admin/images/noupload.png'">
</div>
<!--颜色拾取-->
<div class="form-group no-margin-left no-margin-right mt4 short">
	<div class="input-group">
		<input type="text" class="form-control"  data-arwc="colorpicker" name="xx" value="" data-color-format="hex">
	</div>
</div>
 */
AutoRenderWebCompent.UpOneImg=function(){
	//编辑器设置
	var uploadEditor = KindEditor.editor({
		uploadJson : '/shop/admin/config/general?action=uploadImg',
		allowFileManager: false,
	});
	//显示图片上传对话框
	function showImageUploadDialog($imageId, $hiddenId) {
		uploadEditor.loadPlugin('image', function() {
			uploadEditor.plugin.imageDialog({
                showRemote:false,
				imageUrl: $hiddenId.val(),
				clickFn: function(url, title, width, height, border, align) {
					$hiddenId.val(url);
					$imageId.attr("src", url);
					uploadEditor.hideDialog();
				}
			});
		});
	}
	//btnId:上传按钮的对象, imageId预览图片的对象, hiddenId设置表单值的对象
	this.initImageUploadDialog=function($btnId, $imageId, $hiddenId) {
		$btnId.on("click", function(e) {
			showImageUploadDialog($imageId, $hiddenId);
		});
	}
}
AutoRenderWebCompent.init=function($targetBody){
	General.extendDialog(); //扩展对话框
	//单图上传
	$targetBody.find("div[data-arwc='upOneImg']").each(function(){
		var THIS=$(this);
		var $btn=THIS.find("button[data-arwcId='btnUpload']");
		var $image=THIS.find("img[data-arwcId='previewImage']");
		$image.css('max-width','430px');
		var $text=THIS.find("input[data-arwcId='textImgUrl']");
		var arwc=new AutoRenderWebCompent.UpOneImg();
		arwc.initImageUploadDialog($btn,$image,$text);
	});
	//颜色拾取
	$targetBody.find("input[data-arwc='colorpicker']").ColorPickerSliders({
	     placement: 'top',
	     flat: true,
	     sliders: false,
         swatches: false,
       	 hsvpanel: true,
       	 previewformat:'hex'
    });
    //点击我，删除我的父类
    $targetBody.find("a[data-arwc='delParent1']").click(function(){
    	//alert('删除我的父类');
    	var THIS=$(this);
    	THIS.parent().remove();
    });
    //点击我，删除我的父类的父类
    $targetBody.find("a[data-arwc='delParent2']").click(function(){
    	//alert('删除我的父类的父类');
    	var THIS=$(this);
    	THIS.parent().parent().remove();
    });
    //点击我，删除相对我的层线的父层
    $targetBody.find("a[data-arwc='delParent']").click(function(){
    	var THIS=$(this);
    	var layer=parseInt(THIS.attr('data-parentLayer'),10);
    	//alert('删除我的父类的层数:'+layer);
    	var p=THIS;
    	for(var i=0;i<layer;i++){
    		p=p.parent();
    	}
    	p.remove();
    	//THIS.parent().parent().remove();
    });
    //选取商品信息
    $targetBody.find("a[data-arwc='selectProImg']").click(function(){
    	var THIS=$(this);
    	var title='选取商品信息';
    	var url=THIS.attr('data-arwcUrl');
    	var height = $(window).height(); //获取窗口的高度
    	General.showDialogWidthHeight(title,url,"90%",height);
    	forAautoRenderWebCompent_selectProImg_currentA=THIS;
    });
    var forAautoRenderWebCompent_selectProImg_currentA=null;//用于保存选取商品信息时，当前的selectProImg 按钮
    window.forAautoRenderWebCompent_selectProImg=function(msg){
    	//alert('来自加载的页:'+msg);
    	//找到当前的selectProImg 按钮,对应项，设置选择的值
    	var data=msg.split(',');
    	var id=data[0];
    	var title=data[1];
    	var picurl=data[2];
    	var url=data[3];
    	var price=data[4];
    	if(forAautoRenderWebCompent_selectProImg_currentA==null){
    		alert('没有找到传入的值:#1');
    		return;
    	}
    	var $currentA=forAautoRenderWebCompent_selectProImg_currentA;
    	var $input=$currentA.next();
    	$input.val(msg);
    	//设置预览图片
    	//注：如果选取商品信息的单元层级结构换了
    	//alert($currentA.parent().parent().parent().html());
    	try{
    		$currentA.parent().parent().parent().find('.preview-image').attr('src',picurl);
    	}catch(e){
    		alert('没有找到预览图片:#2');
    	}
    	forAautoRenderWebCompent_selectProImg_currentA=null;
    }
}
